package com.accp.www.chat;

import java.util.Date;
import java.util.List;


public abstract class AbstractChatUser implements ChatUser, Authority {

	ChatHouse house;
	ChatRoom currentRoom;
	Date inRoomTime;
	String name;

	public AbstractChatUser() {
	}

	public AbstractChatUser(String name) {
		this.name=name;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void inRoom(String roomName) {
		if(this.currentRoom!=null){
			this.outRoom();
		}
		ChatRoom newRoom=house.get(roomName);
		this.currentRoom=newRoom;
		this.inRoomTime=new Date();
		currentRoom.addUser(this);
	}

	@Override
	public void inHouse(ChatHouse chatHouse) {
		this.house=chatHouse;
	}

	@Override
	public void speak(String messageText) {
		ChatMessage chatMsg=new ChatMessage(this,messageText);
		currentRoom.addMessage(chatMsg);
	}

	@Override
	public boolean speak(String userName, String messageText) {
		ChatUser otherChatUser=this.currentRoom.getUserByName(userName);
		if(otherChatUser==null){
			return false;
		}
		ChatMessage chatMsg=new ChatMessage(this,otherChatUser,messageText);
		currentRoom.addMessage(chatMsg);
		return true;
	}

	@Override
	public List<ChatMessage> getMessages() {
		return this.currentRoom.getMessages(this);
	}

	@Override
	public Date getInRoomTime() {
		return inRoomTime;
	}

	@Override
	public List<ChatUser> getRoomUsers() {
		return this.currentRoom.getUsers();
	}

	@Override
	public boolean outRoom() {
		boolean ret=this.currentRoom.removeUser(this);
		this.currentRoom=null;
		return ret;
	}

	@Override
	public ChatRoom getCurrentRoom() {
		return this.currentRoom;
	}
}
